<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the PDODB_Date class.
 *
 * PHP version 5
 *
 * This file is part of PEAR::PDODB - A database object relational management
 * layer for PHP 5 built using PDO. PHP version 5.1+ is recommended.
 *
 * PEAR::PDODB is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * PEAR::PDODB is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with PEAR::PDODB. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     PDODB
 * @category    Database
 * @author      Lukas Smith <smith@pooteeweet.org>
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   1998-2007 Manuel Lemos, Tomas V.V. Cox, Stig S. Bakken, Lukas Smith
 * @copyright   2007 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Date.php 2 2007-11-27 17:20:29Z mjijackson $
 * @link        http://pear.php.net/packages/PDODB
 */

/**
 * This class contains several static methods that convert the PDODB native
 * timestamp (ISO based) to and from data structures that are convenient to
 * work with in PHP.
 *
 * @package     PDODB
 * @category    Database
 * @author      Lukas Smith <smith@pooteeweet.org>
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class PDODB_Date
{

    /**
     * Returns the current timestamp in the PDODB format.
     *
     * @return  string      The current timestamp
     * @access  public
     * @static
     */
    public static function timestamp()
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * Returns the current date in the PDODB format.
     *
     * @return  string      The current date
     * @access  public
     * @static
     */
    public static function date()
    {
        return date('Y-m-d');
    }

    /**
     * Returns the current time in the PDODB format.
     *
     * @return  string      The current time
     * @access  public
     * @static
     */
    public static function time()
    {
        return date('H:i:s');
    }

    /**
     * Converts a date into the PDODB timestamp format.
     *
     * @param   int     $hour       The hour of the date
     * @param   int     $minute     The minute of the date
     * @param   int     $second     The second of the date
     * @param   int     $month      The month of the date
     * @param   int     $day        The day of the date
     * @param   int     $year       The year of the date
     * @return  string              The timestamp
     * @access  public
     * @static
     */
    public static function date2timestamp($hour = null, $minute = null,
        $second = null, $month = null, $day = null, $year = null)
    {
        return PDODB_Date::unix2timestamp(mktime($hour, $minute, $second, $month,
            $day, $year, -1));
    }

    /**
     * Converts a PDODB timestamp into an array containing all values necessary
     * to pass to {@link http://php.net/date date()}.
     *
     * @param   string  $timestamp      A valid PDODB timestamp
     * @return  array                   An associative array containing the time
     *                                  values
     * @access  public
     * @static
     */
    public static function timestamp2date($timestamp)
    {
        list($arr['year'], $arr['month'], $arr['day'], $arr['hour'],
            $arr['minute'], $arr['second']) = sscanf($timestamp, "%04u-%02u-%02u %02u:%02u:%02u");
        return $arr;
    }

    /**
     * Converts a UNIX timestamp into a PDODB timestamp.
     *
     * @param   int     $timestamp      A valid UNIX timestamp
     * @return  string                  A valid PDODB timestamp
     * @access  public
     * @static
     */
    public static function unix2timestamp($timestamp)
    {
        return date('Y-m-d H:i:s', $timestamp);
    }

    /**
     * Converts a PDODB timestamp into a UNIX timestamp.
     *
     * @param   string  $timestamp      A valid PDODB timestamp
     * @return  string                  A valid UNIX timestamp
     * @access  public
     * @static
     */
    public static function timestamp2unix($timestamp)
    {
        $arr = PDODB_Date::timestamp2date($timestamp);
        return mktime($arr['hour'], $arr['minute'], $arr['second'], $arr['month'],
            $arr['day'], $arr['year'], -1);
    }

}

?>
